﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Media;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using Microsoft.Phone.Maps.Controls;
using SkiTracker.Model;

namespace SkiTracker.ViewModel
{
    public  class MapViewModel : ViewModelBase
    {
        public MapViewModel()
        {

            _modes = new ObservableCollection<MapMode>
                {
                    new MapMode {CartographicMode = MapCartographicMode.Road, Name = "Road"},
                    new MapMode {CartographicMode = MapCartographicMode.Aerial, Name = "Aerial"},
                    new MapMode {CartographicMode = MapCartographicMode.Hybrid, Name = "Hybrid"},
                    new MapMode {CartographicMode = MapCartographicMode.Terrain, Name = "Terrain"}
                };
            _selectedMode = _modes[0];


        }

        private MapMode _selectedMode;
        public MapMode SelectedMode
        {
            get { return _selectedMode; }
            set
            {
                if (_selectedMode != value)
                {
                    _selectedMode = value;
                    RaisePropertyChanged(() => SelectedMode);
                }
            }
        }

        private ObservableCollection<MapMode> _modes;
        public ObservableCollection<MapMode> Modes
        {
            get { return _modes; }
            set
            {
                if (_modes != value)
                {
                    _modes = value;
                    RaisePropertyChanged(() => Modes);
                }
            }
        }

        private double _pitch;
        public double Pitch
        {
            get { return _pitch; }
            set
            {
                if (Math.Abs(_pitch - value) > 0.05)
                {
                    _pitch = value;
                    RaisePropertyChanged(() => Pitch);
                }
            }
        }

        private double _heading;
        public double Heading
        {
            get { return _heading; }
            set
            {
                if (value > 180) value -= 360;
                if (value < -180) value += 360;
                if (Math.Abs(_heading - value) > 0.05)
                {
                    _heading = value;
                    DispatcherHelper.CheckBeginInvokeOnUI(() => RaisePropertyChanged(() => Heading));
                }
            }
        }

        private double _zoomLevel = 13;
        public double ZoomLevel
        {
            get { return _zoomLevel; }
            set
            {
                if (Math.Abs(_zoomLevel - value) > 0.001)
                {
                    if (value < 1.0) _zoomLevel = 1.0;
                    else if (value > 20.0) _zoomLevel = 20.0;
                    else _zoomLevel = value;
                    
                    RaisePropertyChanged(() => ZoomLevel);
                }
            }
        }

        private ICommand _zoomInCommand;
        public ICommand ZoomInCommand
        {
            get { return _zoomInCommand ?? (_zoomInCommand = new RelayCommand(() => ZoomLevel += 0.5)); }
        }

        private ICommand _zoomOutCommand;
        public ICommand ZoomOutCommand
        {
            get { return _zoomOutCommand ?? (_zoomOutCommand = new RelayCommand(() => ZoomLevel -= 0.5)); }
        }

        public Color AccentColor { get { return App.CurrentApp.AccentColor; } }

        private IMessenger _messenger;
        public IMessenger Messenger { get { return _messenger ?? (_messenger = new Messenger()); } }

    }
}